home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivlangud.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  12.6 KB  |  502 lines

  1. unit IvLanguD;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, ComCtrls, SysUtils, Classes, Forms, Dialogs, Controls, StdCtrls,
  9.   IvDictio, IvMulti, IvConMod;
  10.  
  11. type
  12.   TIvLanguageDialogType = (ivldtLanguage, ivldtSubLanguage, ivldtLocale);
  13.  
  14.   TIvLanguageDialog = class(TForm)
  15.     TreeView: TTreeView;
  16.     OKButton: TButton;
  17.     CancelButton: TButton;
  18.     HelpButton: TButton;
  19.     Translator: TIvTranslator;
  20.     IvControlModule1: TIvControlModule;
  21.     procedure FormActivate(Sender: TObject);
  22.     procedure TreeViewDblClick(Sender: TObject);
  23.     procedure HelpButtonClick(Sender: TObject);
  24.  
  25.   protected
  26.     function GetSelectedValue: Integer;
  27.  
  28.   public
  29.     constructor CreateParam(
  30.       owner: TComponent;
  31.       dictionary: TIvDictionary;
  32.       const msg: String;
  33.       options: TIvLanguageDialogOptions;
  34.       dialogType: TIvLanguageDialogType;
  35.       helpContext: THelpContext);
  36.  
  37.     property SelectedValue: Integer read GetSelectedValue;
  38.   end;
  39.  
  40.   function SelectLanguage(
  41.     parent: TControl;
  42.     dictionary: TIvDictionary;
  43.     const msg: String;
  44.     options: TIvLanguageDialogOptions;
  45.     helpContext: THelpContext;
  46.     var language: Integer): Boolean;
  47.  
  48.   function SelectSublanguage(
  49.     parent: TControl;
  50.     dictionary: TIvDictionary;
  51.     const msg: String;
  52.     options: TIvLanguageDialogOptions;
  53.     helpContext: THelpContext;
  54.     var langId: Integer): Boolean;
  55.  
  56.   function SelectLocale(
  57.     parent: TControl;
  58.     dictionary: TIvDictionary;
  59.     const msg: String;
  60.     options: TIvLanguageDialogOptions;
  61.     helpContext: THelpContext;
  62.     var locale: Integer): Boolean;
  63.  
  64. implementation
  65.  
  66. {$R *.DFM}
  67.  
  68. function SelectLanguage(
  69.   parent: TControl;
  70.   dictionary: TIvDictionary;
  71.   const msg: String;
  72.   options: TIvLanguageDialogOptions;
  73.   helpContext: THelpContext;
  74.   var language: Integer): Boolean;
  75. var
  76.   dialog: TIvLanguageDialog;
  77. begin
  78.   if not dictionary.IsOpen then
  79.     raise EIvMulti.Create(
  80.       'Dictionary is not open' +
  81.       #10#13'You must open the dictionary before you can change the language');
  82.  
  83.   Result := False;
  84.   dialog := TIvLanguageDialog.CreateParam(
  85.     nil,
  86.     dictionary,
  87.     msg,
  88.     options,
  89.     ivldtLanguage,
  90.     helpContext);
  91.   try
  92.     if not (ivloNoCenter in options) then
  93.       IvCenterControl(parent, dialog);
  94.  
  95.     if dialog.ShowModal = idOK then
  96.     begin
  97.       language := dialog.SelectedValue;
  98.       Result := True;
  99.     end;
  100.   finally
  101.     Screen.Cursor := crDefault;
  102.     dialog.Free;
  103.   end;
  104. end;
  105.  
  106. function SelectSublanguage(
  107.   parent: TControl;
  108.   dictionary: TIvDictionary;
  109.   const msg: String;
  110.   options: TIvLanguageDialogOptions;
  111.   helpContext: THelpContext;
  112.   var langId: Integer): Boolean;
  113. var
  114.   dialog: TIvLanguageDialog;
  115. begin
  116.   if not dictionary.IsOpen then
  117.     raise EIvMulti.Create(
  118.       'Dictionary is not open' +
  119.       #10#13'You must open the dictionary before you can change the language');
  120.  
  121.   Result := False;
  122.   dialog := TIvLanguageDialog.CreateParam(
  123.     nil,
  124.     dictionary,
  125.     msg,
  126.     options,
  127.     ivldtSubLanguage,
  128.     helpContext);
  129.   try
  130.     if not (ivloNoCenter in options) then
  131.       IvCenterControl(parent, dialog);
  132.  
  133.     if dialog.ShowModal = idOK then
  134.     begin
  135.       langId := dialog.SelectedValue;
  136.       Result := True;
  137.     end;
  138.   finally
  139.     Screen.Cursor := crDefault;
  140.     dialog.Free;
  141.   end;
  142. end;
  143.  
  144. function SelectLocale(
  145.   parent: TControl;
  146.   dictionary: TIvDictionary;
  147.   const msg: String;
  148.   options: TIvLanguageDialogOptions;
  149.   helpContext: THelpContext;
  150.   var locale: Integer): Boolean;
  151. var
  152.   dialog: TIvLanguageDialog;
  153. begin
  154.   if not dictionary.IsOpen then
  155.     raise EIvMulti.Create(
  156.       'Dictionary is not open' +
  157.       #10#13'You must open the dictionary before you can change the locale');
  158.  
  159.   Result := False;
  160.   Screen.Cursor := crHourglass;
  161.   dialog := TIvLanguageDialog.CreateParam(
  162.     nil,
  163.     dictionary,
  164.     msg,
  165.     options,
  166.     ivldtLocale,
  167.     helpContext);
  168.   try
  169.     if not (ivloNoCenter in options) then
  170.       IvCenterControl(parent, dialog);
  171.  
  172.     if dialog.ShowModal = idOK then
  173.     begin
  174.       locale := dialog.SelectedValue;
  175.       Result := True;
  176.     end;
  177.   finally
  178.     Screen.Cursor := crDefault;
  179.     dialog.Free;
  180.   end;
  181. end;
  182.  
  183. constructor TIvLanguageDialog.CreateParam(
  184.   owner: TComponent;
  185.   dictionary: TIvDictionary;
  186.   const msg: String;
  187.   options: TIvLanguageDialogOptions;
  188.   dialogType: TIvLanguageDialogType;
  189.   helpContext: THelpContext);
  190. var
  191.   str, name: String;
  192.   found: Boolean;
  193.   i, j, k, first, langId: Integer;
  194.   subs: TStringList;
  195.   locales: TList;
  196.   node: TTreeNode;
  197.   locale: TIvLocale;
  198.   language: TIvLanguage;
  199.  
  200.   procedure AddCountry(node: TTreeNode; locale: TIvLocale);
  201.   var
  202.     str: String;
  203.   begin
  204.     if ivloUseNativeLanguage in options then
  205.     begin
  206.       str := locale.NativeCountryName;
  207.       if locale.Primary = LANG_NORWEGIAN then
  208.         case locale.Sub of
  209.           SUBLANG_NORWEGIAN_BOKMAL: str := str + ' (bokmσl)';
  210.           SUBLANG_NORWEGIAN_NYNORSK: str := str + ' (nynorsk)';
  211.         end;
  212.     end
  213.     else
  214.       str := dictionary.ComposeCountryName(
  215.         locale.EnglishCountryName,
  216.         locale.Primary,
  217.         locale.Sub,
  218.         False,
  219.         nil);
  220.  
  221.     TreeView.Items.AddChildObject(node, str, TObject(locale.Locale));
  222.   end;
  223.  
  224.   procedure SelectNode(locale: Integer);
  225.   var
  226.     node: TTreeNode;
  227.   begin
  228.     // Select the active sublanguage
  229.  
  230.     node := TreeView.TopItem;
  231.     while node <> nil do
  232.     begin
  233.       if IvGetPrimaryFromLocale(locale) = IvGetPrimaryFromLocale(Integer(node.Data)) then
  234.       begin
  235.         // Found the right language. Now finds the right country.
  236.  
  237.         node := node.GetFirstChild;
  238.         while node <> nil do
  239.         begin
  240.           if IvGetSubFromLocale(locale) = IvGetSubFromLocale(Integer(node.Data)) then
  241.           begin
  242.             node.Selected := True;
  243.             Exit;
  244.           end;
  245.  
  246.           // Moves to the next country
  247.  
  248.           node := node.GetNextSibling;
  249.         end;
  250.         Exit;
  251.       end;
  252.  
  253.       // Moves to the next language
  254.  
  255.       node := node.GetNextSibling;
  256.     end;
  257.   end;
  258.  
  259. begin
  260.   inherited Create(owner);
  261.  
  262.   Self.HelpContext := helpContext;
  263.   if helpContext = 0 then
  264.     HelpButton.Hide;
  265.  
  266.   if msg = '' then
  267.     case dialogType of
  268.       ivldtLanguage: Caption := 'Select Language';
  269.       ivldtSubLanguage: Caption := 'Select Sublanguage';
  270.       ivldtLocale: Caption := 'Select Locale';
  271.     end
  272.   else
  273.     Caption := msg;
  274.  
  275.   Translator.Dictionary := dictionary;
  276.   if not (ivloUseNativeLanguage in options) then
  277.     Translator.Targets.Add(TIvTargetProperty.Create('', 'Items', ivttInclude));
  278.  
  279.   case dialogType of
  280.     ivldtLanguage:
  281.     begin
  282.       // Builds up the language list
  283.  
  284.       if dictionary.Languages[0].Primary = LANG_NEUTRAL then
  285.         first := 1
  286.       else
  287.         first := 0;
  288.  
  289.       for i := first to dictionary.LanguageCount - 1 do
  290.       begin
  291.         language := dictionary.Languages[i];
  292.  
  293.         if (not (ivloShowAllLanguages in options)) and
  294.           ((dictionary.CheckLevel = ivclCodePage) and
  295.            (not dictionary.IsLanguageSupportedByCodePage(language))) or
  296.           ((dictionary.CheckLevel = ivclSystem) and
  297.            (not dictionary.IsLanguageSupportedBySystem(language))) then
  298.         begin
  299.           Continue;
  300.         end;
  301.  
  302.         if ivloUseNativeLanguage in options then
  303.           name := dictionary.Languages[i].NativeName
  304.         else
  305.           name := dictionary.Languages[i].EnglishName;
  306.         TreeView.Items.AddObject(nil, name, TObject(i));
  307.       end;
  308.  
  309.       // Translates the dialog
  310.  
  311.       Translator.Translate;
  312. {$IFNDEF VER110}
  313.       TreeView.SortType := stText;
  314. {$ENDIF}
  315.  
  316.       // Select the active language
  317.  
  318.       for i := 0 to TreeView.Items.Count - 1 do
  319.       begin
  320.         if dictionary.ActiveLanguage = Integer(TreeView.Items[i].Data) then
  321.         begin
  322.           TreeView.Items[i].Selected := True;
  323.           Break;
  324.         end;
  325.       end;
  326.     end;
  327.  
  328.     ivldtSublanguage:
  329.     begin
  330.       if dictionary.Languages[0].Primary = LANG_NEUTRAL then
  331.         first := 1
  332.       else
  333.         first := 0;
  334.  
  335.       for i := first to dictionary.LanguageCount - 1 do
  336.       begin
  337.         language := dictionary.Languages[i];
  338.  
  339.         if (not (ivloShowAllLanguages in options)) and
  340.           ((dictionary.CheckLevel = ivclCodePage) and
  341.            (not dictionary.IsLanguageSupportedByCodePage(language))) or
  342.           ((dictionary.CheckLevel = ivclSystem) and
  343.            (not dictionary.IsLanguageSupportedBySystem(language))) then
  344.         begin
  345.           Continue;
  346.         end;
  347.  
  348.         // Checks if the language already exist in the tree
  349.  
  350.         node := nil;
  351.         for j := 0 to TreeView.Items.Count - 1 do
  352.         begin
  353.           if Integer(TreeView.Items[j].Data) = language.Primary then
  354.           begin
  355.             node := TreeView.Items[j];
  356.             Break;
  357.           end;
  358.         end;
  359.  
  360.         // If not found, adds the language to the tree
  361.  
  362.         if node = nil then
  363.         begin
  364.           if ivloUseNativeLanguage in options then
  365.             name := language.NativeName
  366.           else
  367.             name := language.EnglishName;
  368.           node := TreeView.Items.AddObject(nil, name, TObject(language.Primary));
  369.         end;
  370.  
  371.         // Adds the locales of the language to the tree
  372.  
  373.         subs := TStringList.Create;
  374.         dictionary.GetSubLanguages(language, subs, ivloUseNativeLanguage in options);
  375.         for j := 0 to subs.Count - 1 do
  376.         begin
  377.           // Checks if the tree already contains the locale
  378.  
  379.           found := False;
  380.           langId := Integer(subs.Objects[j]);
  381.           for k := 0 to TreeView.Items.Count - 1 do
  382.           begin
  383.             if Integer(TreeView.Items[k].Data) = langId then
  384.             begin
  385.               found := True;
  386.               Break;
  387.             end;
  388.           end;
  389.  
  390.           // If not, adds the locale to the tree
  391.  
  392.           if not found then
  393.             TreeView.Items.AddChildObject(node, subs[j], TObject(langId));
  394.         end;
  395.         subs.Free;
  396.       end;
  397.  
  398.       // Translates the dialog
  399.  
  400.       Translator.Translate;
  401.       TreeView.SortType := stText;
  402.       SelectNode(dictionary.LanguageLocale);
  403.     end;
  404.  
  405.     ivldtLocale:
  406.     begin
  407.       // Dialog shows all available locales
  408.  
  409.       locales := TList.Create;
  410.       dictionary.GetLocales(locales);
  411.  
  412.       for i := 0 to locales.Count - 1 do
  413.       begin
  414.         locale := TIvLocale(locales[i]);
  415.         if (not (ivloShowAllLanguages in options)) and
  416.           ((dictionary.CheckLevel = ivclCodePage) and
  417.            (not dictionary.IsLocaleSupportedByCodePage(locale))) or
  418.           ((dictionary.CheckLevel = ivclSystem) and
  419.            (not IvIsCodePageSupportedBySystem(locale.CodePage))) then
  420.         begin
  421.           Continue;
  422.         end;
  423.  
  424.         // Checks of the tree view already contains the language of the locale.
  425.  
  426.         found := False;
  427.         node := TreeView.TopItem;
  428.         while node <> nil do
  429.         begin
  430.           if Integer(node.Data) = locale.Primary then
  431.           begin
  432.             // Yes. Adds the country
  433.  
  434.             AddCountry(node, locale);
  435.             found := True;
  436.             Break;
  437.           end;
  438.  
  439.           node := node.GetNextSibling;
  440.         end;
  441.  
  442.         // If not found adds a new language and country.
  443.  
  444.         if not found then
  445.         begin
  446.           if ivloUseNativeLanguage in options then
  447.             str := locale.NativeLanguageName
  448.           else
  449.             str := locale.EnglishLanguageName;
  450.  
  451.           node := TreeView.Items.AddObject(
  452.             nil,
  453.             TIvDictionary.ComposeLanguageName(
  454.               str,
  455.               locale.Primary,
  456.               locale.CodePage,
  457.               False,
  458.               nil),
  459.             TObject(locale.Primary));
  460.           AddCountry(node, locale);
  461.         end;
  462.       end;
  463.  
  464.       dictionary.FreeList(locales);
  465.  
  466.       // Translates the dialog
  467.  
  468.       Translator.Translate;
  469.       TreeView.SortType := stText;
  470.       SelectNode(dictionary.Locale);
  471.     end;
  472.   end;
  473. end;
  474.  
  475. function TIvLanguageDialog.GetSelectedValue: Integer;
  476. begin
  477.   Result := Integer(TreeView.Selected.Data);
  478. end;
  479.  
  480. procedure TIvLanguageDialog.FormActivate(Sender: TObject);
  481. begin
  482.   if HelpContext = 0 then
  483.     HelpButton.Hide;
  484. end;
  485.  
  486. procedure TIvLanguageDialog.HelpButtonClick(Sender: TObject);
  487. begin
  488.   Application.HelpContext(HelpContext);
  489. end;
  490.  
  491. procedure TIvLanguageDialog.TreeViewDblClick(Sender: TObject);
  492. begin
  493.   if (TreeView.Selected = nil) or (TreeView.Selected.Count > 0) then
  494.     Exit;
  495.  
  496.   Close;
  497.   ModalResult := idOK;
  498. end;
  499.  
  500. end.
  501.  
  502.